package com.own.component.sharding.util;

import com.own.component.common.util.PropertiesYamlUtil;
import com.own.component.util.database.DatabaseUtil;
import com.own.component.util.database.model.DatabaseConfigModel;
import com.own.component.util.database.mysql.MysqlProperty;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * ShardingUtil
 *
 * @author chenxueli
 * @date 2023-07-19 16:49:00
 */
@Component
public class ShardingUtil {

    /**
     * 查询所有的表名
     *
     * @return 表名集合
     */
    public Set<String> listTableNames() {
        var config = DatabaseConfigModel.builder()
                .url(PropertiesYamlUtil.get("spring.shardingsphere.datasource.ds1.jdbc-url"))
                .username(PropertiesYamlUtil.get("spring.shardingsphere.datasource.ds1.username"))
                .password(PropertiesYamlUtil.get("spring.shardingsphere.datasource.ds1.password"))
                .database(PropertiesYamlUtil.get("spring.shardingsphere.database.name"))
                .build();
        // 查询数据库中所有的表数据
        var util = DatabaseUtil.instance(
                config,
                MysqlProperty.instance()
        );
        return util.listTableNames();
    }

}
